package com.confinderence;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import com.confinderence.WebServiceTask.IResponseHandler;
import com.confinderence.model.Conference;
import com.confinderence.model.Lecture;
import com.confinderence.model.Lecturer;
import com.confinderence.model.TransferConference;
import com.confinderence.model.User;
import com.google.gson.Gson;

public class CreateLectureActivity extends Activity implements IResponseHandler {
	private EditText mLectureTitle;
	private EditText mDescription;
	private EditText mDate;
	private EditText mTime;
	private EditText mDuration;
	private Button mAddLecture;
	public final static String CONFERENCE = "conference";
	private Conference mConference;
	private static final String URL = "http://" + Utils.IP + "/ConferenceManager/rest/lecture";

	private WebServiceTask mWebTask;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_lecture);
		mConference = (Conference) getIntent().getExtras().getSerializable(CONFERENCE);
		init();
	}

	private void init() {
		mLectureTitle = (EditText) findViewById(R.id.titleLectureEditView);
		mDescription = (EditText) findViewById(R.id.descriptionEditView);
		mDate = (EditText) findViewById(R.id.dateEditView);
		mDate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startDatePickerDialog();
			}
		});
		mTime = (EditText) findViewById(R.id.timeEditView);
		mTime.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startTimePickerDialog();
			}
		});
		mDuration = (EditText) findViewById(R.id.durationInMinutesEditView);
		mAddLecture = (Button) findViewById(R.id.addLectureButon);
		mAddLecture.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				addLecture();
			}
		});
	}

	protected void startTimePickerDialog() {
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		TimePickerDialog pickerDialog = new TimePickerDialog(this, new OnTimeSetListener() {

			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				mTime.setText(String.format("%02d:%02d", hourOfDay, minute));
			}
		}, hour, minute, true);
		pickerDialog.show();
	}

	protected void startDatePickerDialog() {
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int day = c.get(Calendar.DAY_OF_MONTH);
		int month = c.get(Calendar.MONTH);
		DatePickerDialog pickerDialog = new DatePickerDialog(this, new OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				Calendar current = Calendar.getInstance();
				current.set(Calendar.YEAR, year);
				current.set(Calendar.MONTH, monthOfYear);
				current.set(Calendar.DAY_OF_MONTH, dayOfMonth);
				mDate.setText(new SimpleDateFormat("E, MMM d, yyyy").format(current.getTime()));
			}
		}, year, month, day);
		pickerDialog.show();
	}
	protected void addLecture() {
		String title = mLectureTitle.getText().toString();
		String description = mDescription.getText().toString();
		String date = mDate.getText().toString();
		String time = mTime.getText().toString();
		String length = mDuration.getText().toString();
		TransferConference tc = new TransferConference(title, description, new Date(), time,
				Integer.parseInt(length), mConference.getName());
		mWebTask = new WebServiceTask(WebServiceTask.POST_TASK_WITH_JSON, this,
				getString(R.string.wait_msg), this);
		Gson gson = new Gson();
		String json = gson.toJson(tc);
		json = Utils.TRANS_CONF_KEY + json + "}";
		mWebTask.setJson(json);
		mWebTask.execute(URL);
		Utils.goToConferences(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.logged_in_menu, menu);
		Utils.initMenu(menu, this);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void handleResponse(String response) {

	}
}
